package com.yk.spark.model.app.state.process;

import org.json.JSONException;
import org.json.JSONObject;

public class AppProcessState {

    private String action;
    private String driverState;
    private String serverSparkVersion;
    private String submissionId;
    private boolean success;
    private String workerHostPort;
    private String workerId;

    public AppProcessState() {
    }

    public AppProcessState(String processResponse) {
        try {
            JSONObject response=new JSONObject(processResponse);

            if(response.has("action")
                    &&response.has("driverState")
                    &&response.has("serverSparkVersion")
                    &&response.has("submissionId")
                    &&response.has("success")
                    &&response.has("workerHostPort")
                    &&response.has("workerId")){
                this.setAction(response.getString("action"));
                this.setDriverState(response.getString("driverState"));
                this.setServerSparkVersion(response.getString("serverSparkVersion"));
                this.setSubmissionId(response.getString("submissionId"));
                this.setSuccess(response.getBoolean("success"));
                this.setWorkerHostPort(response.getString("workerHostPort"));
                this.setWorkerId(response.getString("workerId"));
            }else {
                throw new NullPointerException();
            }
        } catch (JSONException | NullPointerException e) {
            e.printStackTrace();
        }
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String getDriverState() {
        return driverState;
    }

    public void setDriverState(String driverState) {
        this.driverState = driverState;
    }

    public String getServerSparkVersion() {
        return serverSparkVersion;
    }

    public void setServerSparkVersion(String serverSparkVersion) {
        this.serverSparkVersion = serverSparkVersion;
    }

    public String getSubmissionId() {
        return submissionId;
    }

    public void setSubmissionId(String submissionId) {
        this.submissionId = submissionId;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getWorkerHostPort() {
        return workerHostPort;
    }

    public void setWorkerHostPort(String workerHostPort) {
        this.workerHostPort = workerHostPort;
    }

    public String getWorkerId() {
        return workerId;
    }

    public void setWorkerId(String workerId) {
        this.workerId = workerId;
    }

    @Override
    public String toString() {
        return "AppProcessState{" +
                "action='" + action + '\'' +
                ", driverState='" + driverState + '\'' +
                ", serverSparkVersion='" + serverSparkVersion + '\'' +
                ", submissionId='" + submissionId + '\'' +
                ", success=" + success +
                ", workerHostPort='" + workerHostPort + '\'' +
                ", workerId='" + workerId + '\'' +
                '}';
    }
}
